package com.pn.controller;

import com.pn.dto.RoleAuthArray;
import com.pn.entity.CurrentUser;
import com.pn.entity.Result;
import com.pn.entity.Role;
import com.pn.page.Page;
import com.pn.service.RoleAuthService;
import com.pn.service.RoleService;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "角色信息模块")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private RoleAuthService roleAuthService;
    @Resource
    private TokenUtils tokenUtils;


    @RequestMapping("/role-list")
    public Result findAllRole(){
        List<Role> allRole = roleService.findAllRole();
        return Result.ok(allRole);
    }
    //分页查询角色
    @ApiOperation(value = "分页查询角色")
    @RequestMapping("/role-page-list")
    public Result findRolePage(Role role, Page page){
        Page pageResult = roleService.queryRoleByPage(role, page);
        return Result.ok(page);
    }

    //添加角色信息
    @RequestMapping("/role-add")
    public Result addRole(@RequestBody Role role, @RequestHeader(value = WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        role.setCreateBy(currentUser.getUserId());
        Result result = roleService.addRole(role);
        return result;
    }
    //启用或禁用角色
    @RequestMapping("/role-state-update")
    public Result modifyRoleState(@RequestBody Role role){
        int i = roleService.modifyRoleState(role);
        if (i >= 1){
            return Result.ok();
        }else {
         return Result.err(Result.CODE_ERR_BUSINESS,"角色启用或禁用设置失败!");
        }
    }

    //删除角色信息
    @RequestMapping("/role-delete/{roleId}")
    public Result deleteRole(@PathVariable(value = "roleId") Integer roleId){
        int result = roleService.deleteRole(roleId);
        if (result >= 1){
            return Result.ok("角色删除成功！");
        }
        else {
            return Result.err(Result.CODE_ERR_BUSINESS,"角色删除失败！");
        }
    }
    //根据roleid查询authid
    @RequestMapping("/role-auth")
    public Result findAuthByRoleId(Integer roleId){
        List<Integer> authList = roleAuthService.findAidByRid(roleId);
        return Result.ok(authList);
    }
    //修改角色权限
    @RequestMapping("/auth-grant")
    public Result modifyRoleAuth(@RequestBody RoleAuthArray roleAuthArray){
        int result = roleAuthService.modifyRoleAuth(roleAuthArray.getRoleId(), roleAuthArray.getAuthIds());
        if (result >= 1){
            return Result.ok("角色权限修改成功！");
        }else {
            return Result.err(Result.CODE_ERR_BUSINESS,"角色权限修改失败！");
        }
    }
    //根据roleId修改roleDesc
    @RequestMapping("/role-update")
    public Result modifyRoleDescByRid(@RequestBody Role role,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int userId = currentUser.getUserId();
        int result = roleService.modifyRoleDescByRid(role,userId);
        if (result > 0){
            return Result.ok("角色信息修改成功！");
        }else {
            return Result.err(Result.CODE_ERR_BUSINESS,"角色信息修改失败！");
        }
    }











}
